//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.sending;

import java.math.BigDecimal;
import net.sf.jasperreports.engine.*;
import org.jpgolay.me.agency.Agency;
import org.jpgolay.me.customer.Customer;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.sending:
//            Sending
public class SendingPrintDataSource
        implements JRDataSource {

    public SendingPrintDataSource(Sending sending, Parameter param) {
        this.sending = sending;
        this.param = param;
        getAgency(sending.getAgencyId());
        getCustomer(sending.getCustomerId());
        count = 37;
    }

    public boolean next()
            throws JRException {
        return count > 0;
    }

    public Object getFieldValue(JRField field)
            throws JRException {
        String fieldname = field.getName();
        String value = null;
        if ("boxGTIN".equals(fieldname)) {
            if (sending.getBoxGTIN() != null) {
                value = "N\260 Carton " + sending.getBoxGTIN();
            }
        } else if ("notes".equals(fieldname)) {
            if (sending.getNotes() != null) {
                value = "Observations : " + sending.getNotes();
            }
        } else if ("palletGTIN".equals(fieldname)) {
            if (sending.getPalletGTIN() != null) {
                value = "N\260 Palette : " + sending.getPalletGTIN();
            }
        } else if ("SSCCTxt".equals(fieldname)) {
            if (sending.getSscc() != null) {
                value = "SSCC : " + sending.getSscc();
            }
        } else if ("SSCC".equals(fieldname)) {
            if (sending.getSscc() != null) {
                value = sending.getSscc();
            }
        } else if ("weight".equals(fieldname)) {
            if (sending.getWeight() != null) {
                value = "Poids : " + sending.getWeight().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("preparationDate".equals(fieldname)) {
            if (sending.getPreparationDate() != null) {
                value = "Pr\351par\351 le : " + sending.getPreparationDate();
            }
        } else if ("customerGLN".equals(fieldname)) {
            if (sending.getCustomerGLN() != null) {
                value = "GLN client : " + sending.getCustomerGLN();
            }
        } else if ("customerId".equals(fieldname)) {
            if (sending.getCustomerId() != null) {
                value = "Client : " + sending.getCustomerId();
            }
        } else if ("deliveryForm".equals(fieldname)) {
            if (sending.getDeliveryForm() != null) {
                value = "DESADV (ou n\260 exp) : " + sending.getDeliveryForm();
            }
        } else if ("id".equals(fieldname)) {
            if (sending.getId() != null) {
                value = "(" + sending.getId().toString() + ")";
            }
        } else if ("insuranceCompany".equals(fieldname)) {
            if (sending.getInsuranceCompany() != null) {
                value = "Assurance : " + sending.getInsuranceCompany();
            }
        } else if ("insuranceValue".equals(fieldname)) {
            if (sending.getInsuranceValue() != null) {
                value = "Valeur : " + sending.getInsuranceValue().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
            }
        } else if ("orderId".equals(fieldname)) {
            if (sending.getOrderId() != null) {
                value = "N\260 BL : " + sending.getOrderId();
            }
        } else if ("parcelQuantity".equals(fieldname)) {
            if (sending.getParcelQuantity() != null) {
                value = "Nb de colis : " + sending.getParcelQuantity().setScale(0, param.getRoundType().intValue()).toString();
            }
        } else if ("deliveryAnticipatedDate".equals(fieldname)) {
            if (sending.getDeliveryAnticipatedDate() != null) {
                value = "Livraison pr\351vue le : " + sending.getDeliveryAnticipatedDate();
            }
        } else if ("sendingDate".equals(fieldname)) {
            if (sending.getSendingDate() != null) {
                value = "Remis le : " + sending.getSendingDate();
            }
        } else if ("truckingCompany".equals(fieldname)) {
            if (sending.getTruckingCompagny() != null) {
                value = "Transporteur : " + sending.getTruckingCompagny();
            }
        } else if ("userId".equals(fieldname)) {
            if (sending.getUserId() != null) {
                value = "Pr\351par\351 par : " + sending.getUserId();
            }
        } else if ("agencyCompany".equals(fieldname)) {
            value = agency.getRaisonSociale();
        } else if ("agencyStatus".equals(fieldname)) {
            value = agency.getCivilite();
        } else if ("agencyAddrLn1".equals(fieldname)) {
            value = agency.getAdresseUn();
        } else if ("agencyAddrLn2".equals(fieldname)) {
            value = agency.getAdresseDeux();
        } else if ("agencyZip".equals(fieldname)) {
            value = agency.getCp();
        } else if ("agencyCity".equals(fieldname)) {
            value = agency.getVille();
        } else if ("agencyCountry".equals(fieldname)) {
            value = agency.getPays();
        } else if ("agencyTel1".equals(fieldname)) {
            if (agency.getTelephoneUn() != null) {
                value = "T\351l : " + agency.getTelephoneUn();
            }
        } else if ("agencyFax1".equals(fieldname)) {
            if (agency.getFaxUn() != null) {
                value = "Fax : " + agency.getFaxUn();
            }
        } else if ("customerCompagny".equals(fieldname)) {
            value = customer.getRaisonSociale();
        } else if ("customerStatus".equals(fieldname)) {
            value = customer.getCivilite();
        } else if ("customerAddrLn1".equals(fieldname)) {
            value = getCustomerAddress(0);
        } else if ("customerAddrLn2".equals(fieldname)) {
            value = getCustomerAddress(1);
        } else if ("customerZip".equals(fieldname)) {
            value = getCustomerAddress(2);
        } else if ("customerCity".equals(fieldname)) {
            value = getCustomerAddress(3);
        } else if ("customerCountry".equals(fieldname)) {
            value = getCustomerAddress(4);
        } else if ("customerTel".equals(fieldname)) {
            if (customer.getTelephone() != null) {
                value = "T\351l " + customer.getTelephone();
            }
        } else if ("customerFax".equals(fieldname) && customer.getFax() != null) {
            value = "Fax " + customer.getFax();
        }
        count--;
        return value;
    }

    private void getAgency(Integer agencyId) {
        ObjectCRUD crud = new ObjectCRUD();
        agency = (Agency) crud.read(agencyId, 21);
    }

    private void getCustomer(String customerId) {
        ObjectCRUD crud = new ObjectCRUD();
        customer = (Customer) crud.read(customerId, 27);
    }

    private String getCustomerAddress(int line) {
        switch (line) {
            case 0: // '\0'
                if (!customer.getCpLivr().equals("")) {
                    return customer.getAdresseLivrUn();
                } else {
                    return customer.getAdresseFactUn();
                }

            case 1: // '\001'
                if (!customer.getCpLivr().equals("")) {
                    return customer.getAdresseLivrDeux();
                } else {
                    return customer.getAdresseFactDeux();
                }

            case 2: // '\002'
                if (!customer.getCpLivr().equals("")) {
                    return customer.getCpLivr();
                } else {
                    return customer.getCpFact();
                }

            case 3: // '\003'
                if (!customer.getCpLivr().equals("")) {
                    return customer.getVilleLivr();
                } else {
                    return customer.getVilleFact();
                }

            case 4: // '\004'
                if (!customer.getCpLivr().equals("")) {
                    return customer.getPaysLivr();
                } else {
                    return customer.getPaysFact();
                }
        }
        return null;
    }
    private Sending sending;
    private Agency agency;
    private Customer customer;
    private Parameter param;
    private int count;
}
